package com.smalls.redshoes.util;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.text.format.Time;

import com.smalls.newvideotwo.util.LogUtil;
import com.uking.util.TimeUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil {
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + "DateUtil":"DateUtil";
    private static Date mNetDate;
    private static SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    private static SimpleDateFormat sfd2 = new SimpleDateFormat("yyyyMMdd", Locale.US);
    private String[] mDates = new String[7];

    public static String dateToString() {
        return sfd.format(new Date());
    }

    public static String dateToString(Date date) {
        return sfd.format(date);
    }

    public static String dateToString(Date date, String format) {
        return new SimpleDateFormat(format, Locale.US).format(date);
    }

    public static String stringToStringDateSfd2(String strDate) {
        Date date = null;
        try {
            date = sfd.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sfd2.format(date);
    }

    public static Date stringToDate(String date) {
        try {
            return sfd.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getOneDayLaterDate(String date) {
        Date mDate = stringToDate(date);
        if (mDate == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(mDate);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return dateToString(calendar.getTime());
    }

    public static String getOneDayBeforeDate(String date) {
        Date mDate = stringToDate(date);
        if (mDate == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(mDate);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return dateToString(calendar.getTime());
    }

    public static Date stringToDate(String date, String format) {
        try {
            return new SimpleDateFormat(format, Locale.US).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date formatDate() {
        try {
            return sfd.parse(sfd.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String dateToString2() {
        return sfd2.format(new Date());
    }

    public static String stringToStringDate(String strDate) {
        return dateToString(stringToDate(strDate));
    }

    public static String getTime() {
        return new SimpleDateFormat("HH:mm", Locale.US).format(new Date());
    }

    public static String getHoursAgoTime(int i) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR, 10 - i);
        return new SimpleDateFormat("HH:mm", Locale.US).format(cal.getTime());
    }

    public String getDate(int i, Context context) {
        if (this.mDates[i] == null) {
            Date date = getSysDaysAgoDate(i, context);
            this.mDates[i] = new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(date);
            if (this.mDates[i].substring(0, 4).equals("1970") || this.mDates[i].substring(0, 4).equals("1969")) {
                if (i == 0 && mNetDate == null) {
                    mNetDate = getCurBJTime(context);
                }
                if (mNetDate != null) {
                    this.mDates[i] = getNetDaysAgoDate(i, mNetDate, context);
                }
            }
        }
        return this.mDates[i];
    }

    public Date getSevenDate(int i, Context context) {
        Date date = null;
        if (this.mDates[i] == null) {
            date = getSysDaysAgoDate(i, context);
            this.mDates[i] = new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(date);
            if (this.mDates[i].substring(0, 4).equals("1970") || this.mDates[i].substring(0, 4).equals("1969")) {
                if (i == 0 && mNetDate == null) {
                    mNetDate = getCurBJTime(context);
                }
                if (mNetDate != null) {
                    this.mDates[i] = getNetDaysAgoDate(i, mNetDate, context);
                }
            }
        }
        return date;
    }

    public static Date getSysDaysAgoDate(int i, Context context) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - i);
        return cal.getTime();
    }

    public static String getNetDaysAgoDate(int i, Date curDate, Context context) {
        String mDate = "";
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(curDate);
        cal2.set(Calendar.DAY_OF_YEAR, cal2.get(Calendar.DAY_OF_YEAR) - i);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT 08:00"));
        return dateFormat.format(cal2.getTime());
    }

    public static boolean isNetEnable(Context context) {
        NetworkInfo networkinfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }
        return true;
    }

    public static Date getCurBJTime(Context context) {
        if (isNetEnable(context)) {
            try {
                URLConnection uc = new URL("http://bjtime.cn").openConnection();
                uc.connect();
                return new Date(uc.getDate());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    public static String getDaysAgoDate(int i, Context context) {
        String mDate = "";
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - i);
        mDate = new SimpleDateFormat("yyyy", Locale.US).format(cal.getTime());
        if (mDate == null || mDate.trim().equals("")) {
            return mDate;
        }
        if (!mDate.equals("1970") && !mDate.equals("1969")) {
            return new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(cal.getTime());
        }
        try {
            if (!isNetEnable(context)) {
                return mDate;
            }
            URLConnection uc = new URL("http://www.bjtime.cn").openConnection();
            uc.connect();
            Date netDate = new Date(uc.getDate());
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(netDate);
            cal2.set(Calendar.DAY_OF_YEAR, cal2.get(Calendar.DAY_OF_YEAR) - i);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT 08:00"));
            return dateFormat.format(cal2.getTime());
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return mDate;
        } catch (IOException e2) {
            e2.printStackTrace();
            return mDate;
        }
    }

    public static String getDaysAfterDate(int i) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + i);
        return new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(cal.getTime());
    }

    public static String getWeekOfDate(Date dt, String[] weekDays) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        String date = new SimpleDateFormat("MM-dd", Locale.US).format(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w] + "\n" + date;
    }

    public static long getLongofTimes(String endTime, String startTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
        Date endDate = null;
        Date startDate = null;
        try {
            endDate = sdf.parse(endTime + "00");
            startDate = sdf.parse(startTime + "00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (endDate.getTime() - startDate.getTime()) / 1000;
    }

    public static String showTime(long time) {
        int minute = ((int) time) / 60;
        int hour = minute / 60;
        int second = ((int) time) % 60;
        minute %= 60;
        return String.format("%02d:%02d:%02d", new Object[]{Integer.valueOf(hour), Integer.valueOf(minute), Integer.valueOf(second)});
    }

    public static long timeStringToLong(String strTime) {
        Date time = null;
        try {
            time = new SimpleDateFormat("HH:mm:ss", Locale.US).parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time.getTime() / 1000;
    }

    public static boolean compareTime(String time1, String time2) {
        DateFormat df = new SimpleDateFormat("HH:mm", Locale.US);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(time1));
            c2.setTime(df.parse(time2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c1.compareTo(c2) < 0) {
            return true;
        }
        return false;
    }

    public static long compareTime(String date) {
        SimpleDateFormat dfs = new SimpleDateFormat(TimeUtils.DEFAULT_STR, Locale.US);
        Date begin = null;
        Date end = null;
        try {
            begin = dfs.parse(dfs.format(new Date()));
            end = dfs.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return end.getTime() - begin.getTime();
    }

    public static Date transferDateToLocal(String dateStr, String timeZoneId) {
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
        try {
            sp.setTimeZone(TimeZone.getTimeZone(timeZoneId));
            return sp.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            LogUtil.e("DateUtil","transfer error");
            return null;
        }
    }

    public static Date changeTimeZone(String date, Activity activity) {
        if (TextUtils.isEmpty(date)) {
            return null;
        }
        return transferDateToLocal(date, "GMT+8");
    }

    public static Date changeTimeZone(String date) {
        if (TextUtils.isEmpty(date)) {
            return null;
        }
        return transferDateToLocal(date, "GMT+8");
    }

    public static String getChangedTimeZoneCurrentDate(String changedFormat, Activity activity) {
        Date resultDate = changeTimeZone(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()), activity);
        if (resultDate != null) {
            return new SimpleDateFormat(changedFormat).format(resultDate);
        }
        return null;
    }

    public static int getCurrentWeekDay() {
        Time time = new Time();
        time.setToNow();
        int weekday = time.weekDay;
        if (weekday == 0) {
            return 7;
        }
        return weekday;
    }

    public static String getChineseWeekDay(int day) {
        String chineseWeekDay = "";
        switch (day) {
            case 0 /*0*/:
                return "\u65e5";
            case 1 /*1*/:
                return "\u4e00";
            case 2 /*2*/:
                return "\u4e8c";
            case 3 /*3*/:
                return "\u4e09";
            case 4 /*4*/:
                return "\u56db";
            case 5 /*5*/:
                return "\u4e94";
            case 6 /*6*/:
                return "\u516d";
            case 7 /*7*/:
                return "\u65e5";
            default:
                return chineseWeekDay;
        }
    }

    public static String getCurrentTimeZone() {
        return createGmtOffsetString(true, true, TimeZone.getDefault().getRawOffset());
    }

    public static String createGmtOffsetString(boolean includeGmt, boolean includeMinuteSeparator, int offsetMillis) {
        int offsetMinutes = offsetMillis / 60000;
        char sign = '+';
        if (offsetMinutes < 0) {
            sign = '-';
            offsetMinutes = -offsetMinutes;
        }
        StringBuilder builder = new StringBuilder(9);
        if (includeGmt) {
            builder.append("GMT");
        }
        builder.append(sign);
        appendNumber(builder, 2, offsetMinutes / 60);
        if (includeMinuteSeparator) {
            builder.append(':');
        }
        appendNumber(builder, 2, offsetMinutes % 60);
        return builder.toString();
    }

    private static void appendNumber(StringBuilder builder, int count, int value) {
        String string = Integer.toString(value);
        for (int i = 0; i < count - string.length(); i++) {
            builder.append('0');
        }
        builder.append(string);
    }

    public static int getBetweenDay(Date fDate, Date oDate) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(fDate);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(oDate);
        return aCalendar.get(Calendar.DAY_OF_YEAR) - day1;
    }
}
